package o2o.web.actions.user;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYPageUtils;
import com.jinyou.utils.common.SysCommon;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.user.UserAddressRecordMemory;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.user.UserAddressRecord;
import o2o.dao.PO.user.UserInfo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;
import java.util.stream.Collectors;

/**
 * 用户收货地址记录
 */
@RestController
public class UserAddressRecordAction {


    /**
     * 搜索用户地址
     *
     * @param token
     * @return
     */
    @RequestMapping("/user/address/record/search")
    public String userAddressRecordSearch(String token, String key, Integer page, Integer size) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 10;
        }
        List<UserAddressRecord> userAddressRecordList = UserAddressRecordMemory.getInstance().listOfUser(user.getUsername());
        if (ValidateUtil.isAbsList(userAddressRecordList)) {
            if (ValidateUtil.isNotNull(key)) {
                userAddressRecordList = userAddressRecordList.stream().filter(item -> {
                    if (!SysCommon.hasKeyword(item.getAddress(), key)
                            && !SysCommon.hasKeyword(item.getAddress2(), key)
                            && !SysCommon.hasKeyword(item.getAddress3(), key)
                            && !SysCommon.hasKeyword(item.getTelephone(), key)
                            && !SysCommon.hasKeyword(item.getBuyer(), key)) {
                        return false;
                    }
                    return true;
                }).collect(Collectors.toList());
            }
            Collections.sort(userAddressRecordList, Comparator.comparingLong(UserAddressRecord::getUsageTimes).reversed());
            Map<String, UserAddressRecord> map = new HashMap<>();
            List<UserAddressRecord> recordList = new ArrayList<>();
            userAddressRecordList.forEach(item -> {
                if (ValidateUtil.isNull(item.getAddress()) || (map.get(item.getAddress()) == null)) {
                    if (ValidateUtil.isNotNull(item.getAddress())) {
                        map.put(item.getAddress(), item);
                    }
                    recordList.add(item);
                }
            });
//            Collections.sort(userAddressRecordList, Comparator.comparingLong(UserAddressRecord::getUsageTimes).reversed());
//            JYPageUtils.PageList pageList = JYPageUtils.getList(userAddressRecordList, page, size);
            Collections.sort(recordList, Comparator.comparingLong(UserAddressRecord::getUsageTimes).reversed());
            JYPageUtils.PageList pageList = JYPageUtils.getList(recordList, page, size);
            if (pageList != null && ValidateUtil.isAbsList(pageList.list)) {
                return AjaxResult.successResult(pageList.list, pageList.totalCount);
            }
        }
        return AjaxResult.successResult(null, 0);
    }

}
